package com.xhsj.user.pms.overseas.service;

import com.xhsj.user.hr.base.BaseService;
import com.xhsj.user.pms.overseas.dto.PatentListByCountryNameDto;
import com.xhsj.user.pms.overseas.entity.TbPamOverseasCountryDict;
import com.xhsj.user.pms.overseas.mapper.TbPamOverseasCountryDictMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.pms.patent.mapper.TbPamOverseasApplyMapper;
import com.xhsj.user.pms.patent.query.PatentListByCountryNameQuery;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 * 专利海外国家字典 服务实现类
 * </p>
 *
 * @author liuqinghua
 * @since Jun 1, 2021 8:22:26 AM
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TbPamOverseasCountryDictService extends BaseService<TbPamOverseasCountryDictMapper, TbPamOverseasCountryDict> {
    @Autowired
    private TbPamOverseasCountryDictMapper dao;

    @Autowired
    public TbPamOverseasApplyMapper tbPamOverseasApplyMapper;

	/**
	 * 保存
	 * @author liuqinghua
	 * @date Jun 1, 2021 8:22:26 AM
	 */
    @Override
    public boolean save(TbPamOverseasCountryDict entity) {
        return super.save(entity);
    }

	/**
	 * 通过id获取数据
	 * @author liuqinghua
	 * @date Jun 1, 2021 8:22:26 AM
	 */
    @Override
    public TbPamOverseasCountryDict getById(Serializable id) {
        return super.getById(id);
    }

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Jun 1, 2021 8:22:26 AM
	 */
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

	/**
	 * 分页查询数据
	 * @author liuqinghua
	 * @date Jun 1, 2021 8:22:26 AM
	 */
    public IPage<TbPamOverseasCountryDict> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbPamOverseasCountryDict entity = JSON.parseObject(JSON.toJSONString(data), TbPamOverseasCountryDict.class);
        Page<TbPamOverseasCountryDict> page = new Page(currentPage,pageSize);
        QueryWrapper<TbPamOverseasCountryDict> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

	/**
	 * 查询所有数据
	 * @author liuqinghua
	 * @date Jun 1, 2021 8:22:26 AM
	 */
    @Override
    public List<TbPamOverseasCountryDict> selectAll() {
        return super.selectAll();
    }

   /**
       * @Description: 删除
       * @Param:  
       * @return:  
       * @Author: liuqinghua
       * @Date: 2021/6/1
       */
    public String deleteById(Long id) throws Exception {
        // 判断是否有关联的案件
        PatentListByCountryNameDto data = new PatentListByCountryNameDto();
        data.setCountryDictId(id);
        if (tbPamOverseasApplyMapper.getPatentListByCountryName(data).size() > 0)
            return "有关联的海外案件,不能删除!";
        // 删除
        if(dao.deleteCountryById(id) != 1)
            throw new Exception("删除失败");
        return "删除成功";
    }

       /**
           * @Description: 修改+案件
           * @Param:
           * @return:
           * @Author: liuqinghua
           * @Date: 2021/6/2
           */
    public void updateCountryDictById(TbPamOverseasCountryDict tbPamOverseasCountryDict) throws Exception {
        // 修改关联的案件
        tbPamOverseasApplyMapper.updateOverseasPatent(tbPamOverseasCountryDict.getName(),tbPamOverseasCountryDict.getId());
        // 修改国家
        if(!super.updateById(tbPamOverseasCountryDict))
            throw new Exception("修改失败");
    }
}
